package cc.ilzy.mall.user.controller;

import cc.ilzy.mall.api.controller.IUserController;
import cc.ilzy.mall.model.pojo.User;
import cc.ilzy.mall.model.request.UserLoginRequest;
import cc.ilzy.mall.model.request.UserRegisterRequest;
import cc.ilzy.mall.user.service.IFileService;
import cc.ilzy.mall.user.service.IUserService;
import cc.ilzy.mall.utils.ApiResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author ilzy
 * @since 2025-06-19
 */
@RestController
public class UserController implements IUserController {

    @Resource
    private IUserService userService;

    @Autowired
    private IFileService fileService;

    @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息")
    @GetMapping("/getUserInfo/{userId}")
    @Override
    public ApiResponse<User> getUserInfo(@PathVariable("userId") Long userId) {
        return ApiResponse.success(userService.getUserInfo(userId));
    }

    @ApiOperation(value = "获取用户分页列表", notes = "获取用户分页列表")
    @GetMapping("/getUserList")
    public ApiResponse<Page<User>> getUserList() {
        return ApiResponse.success(userService.getPageList(1, 1));
    }

    @ApiOperation("上传用户头像")
    @PostMapping("/upload")
    @Override
    public ApiResponse<String> uploadUserAvatar(
            @ApiParam(value = "文件上传", required = true) @RequestParam("file") MultipartFile file) {
        String path = fileService.uploadUserHeaderImg(file);
        return ApiResponse.success(path);
    }

    @Override
    public ApiResponse<Void> registerUser(
            @ApiParam(value = "用户信息", required = true) @RequestBody UserRegisterRequest user) {
        userService.registerUser(user);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse<Void> login(UserLoginRequest user) {
        String token = userService.login(user);
        return ApiResponse.success(token);
    }
}




















